﻿//========================================
// All rights reserved (C) 2017-2100
// CLR 版本：	4.0.30319.42000
// 命名空间:	Common.XmlHandle
// 文 件 名：	XmlTemp
// 作    者：	WooG
// 联系方式：	wooglle@163.com
// 创建时间：	2018/3/12 23:01:53
//========================================
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace Common.XmlHandle
{
    public class XmlTemp
    {
        private XmlDocument xdoc = new XmlDocument();
        private string xmlPath = string.Empty;
        private XmlNode root = null;
        public XmlTemp(string path)
        {
            xmlPath = path;
            try
            {
                xdoc = new XmlDocument();
                xdoc.Load(path);
                root = xdoc.SelectSingleNode("Temp");
            }
            catch (DirectoryNotFoundException)
            {
                Directory.CreateDirectory(path.Substring(0, path.LastIndexOf(@"\")));
                XmlDeclaration dec = xdoc.CreateXmlDeclaration("1.0", "utf-8", null);
                xdoc.AppendChild(dec);
                xdoc.AppendChild(xdoc.CreateElement("Temp"));
                root = xdoc.SelectSingleNode("Temp");
                xdoc.Save(path);
            }
            catch (FileNotFoundException)
            {
                XmlDeclaration dec = xdoc.CreateXmlDeclaration("1.0", "utf-8", null);
                xdoc.AppendChild(dec);
                xdoc.AppendChild(xdoc.CreateElement("Temp"));
                root = xdoc.SelectSingleNode("Temp");
                xdoc.Save(path);
            }
            catch (XmlException)
            {
                xdoc = new XmlDocument();
                XmlDeclaration dec = xdoc.CreateXmlDeclaration("1.0", "utf-8", null);
                xdoc.AppendChild(dec);
                xdoc.AppendChild(xdoc.CreateElement("Temp"));
                root = xdoc.SelectSingleNode("Temp");
                xdoc.Save(path);
            }
            catch
            {
                throw;
            }
            finally
            {
                xdoc.Load(path);
                root = xdoc.SelectSingleNode("Temp");
            }
        }

        public bool IsEmpty()
        {
            xdoc.Load(xmlPath);
            root = xdoc.SelectSingleNode("Temp");
            XmlNode node_err = root.SelectSingleNode("DataErr");
            if (node_err != null && node_err.HasChildNodes)
            {
                return false;
            }
            XmlNode node_cache = root.SelectSingleNode("DataCache");
            if (node_cache != null && node_cache.HasChildNodes)
            {
                return false;
            }
            return true;
        }

        public void AddDataErr(ClassData classData)
        {
            xdoc.Load(xmlPath);
            root = xdoc.SelectSingleNode("Temp");
            XmlNode node = root.SelectSingleNode("DataErr");
            if(node == null)
            {
                node = xdoc.CreateElement("DataErr");
                root.AppendChild(node);
                xdoc.Save(xmlPath);
            }
            AddData2Node(classData, node);
        }

        public void AddDataCache(ClassData classData)
        {
            xdoc.Load(xmlPath);
            root = xdoc.SelectSingleNode("Temp");
            XmlNode node = root.SelectSingleNode("DataCache");
            if (node == null)
            {
                node = xdoc.CreateElement("DataCache");
                root.AppendChild(node);
                xdoc.Save(xmlPath);
            }
            AddData2Node(classData, node);
        }

        public List<ClassData> GetDataErr()
        {
            xdoc.Load(xmlPath);
            root = xdoc.SelectSingleNode("Temp");
            XmlNode node = root.SelectSingleNode("DataErr");
            if (node == null || !node.HasChildNodes)
            {
                return null;
            }
            else
            {
                List<ClassData> b = new List<ClassData>(5);
                foreach(XmlNode nod in node.ChildNodes)
                {
                    b.Add(getDataFormNode(nod));
                }
                return b;
            }
        }

        public List<ClassData> GetDataCache()
        {
            xdoc.Load(xmlPath);
            root = xdoc.SelectSingleNode("Temp");
            XmlNode node = root.SelectSingleNode("DataCache");
            if (node == null || !node.HasChildNodes)
            {
                return null;
            }
            else
            {
                List<ClassData> b = new List<ClassData>(5);
                foreach (XmlNode nod in node.ChildNodes)
                {
                    b.Add(getDataFormNode(nod));
                }
                return b;
            }
        }

        public void RemoveDataErr(ClassData classData)
        {
            xdoc.Load(xmlPath);
            root = xdoc.SelectSingleNode("Temp");
            XmlNode node = root.SelectSingleNode("DataErr");
            if(node == null)
            {
                return;
            }
            for (int i = node.ChildNodes.Count - 1; i >= 0; i--)
            {
                ClassData data = getDataFormNode(node.ChildNodes[i]);
                if (classData.IsSame(data))
                {
                    node.ChildNodes[i].ParentNode.RemoveChild(node.ChildNodes[i]);
                }
            }
            xdoc.Save(xmlPath);
        }

        public void RemoveDataCache(ClassData classData)
        {
            xdoc.Load(xmlPath);
            root = xdoc.SelectSingleNode("Temp");
            XmlNode node = root.SelectSingleNode("DataCache");
            if(node == null)
            {
                return;
            }
            for(int i = node.ChildNodes.Count - 1; i >= 0; i--)
            {
                ClassData data = getDataFormNode(node.ChildNodes[i]);
                if (classData.IsSame(data))
                {
                    node.ChildNodes[i].ParentNode.RemoveChild(node.ChildNodes[i]);
                }
            }
            xdoc.Save(xmlPath);
        }

        private void AddData2Node(ClassData data, XmlNode xmlNode)
        {
            XmlElement node = xdoc.CreateElement("Case");
            node.SetAttribute("ID", data.ID.ToString());
            node.SetAttribute("Code", data.Code);
            xmlNode.AppendChild(node);
            XmlElement ele = xdoc.CreateElement("Info");
            ele.SetAttribute("CaseType", data.CaseType.ToString());
            ele.SetAttribute("Brigade", data.Brigade.ToString());
            ele.SetAttribute("Contact", data.Contact);
            ele.SetAttribute("User", data.User.ToString());
            ele.SetAttribute("TimeHappen", data.TimeHappen.ToString("yyyy/MM/dd HH:mm"));
            ele.SetAttribute("TimeEntrust", data.TimeEntrust.ToString("yyyy/MM/dd"));
            ele.SetAttribute("TimeArrange", data.TimeArrange.ToString("yyyy/MM/dd HH:mm:ss"));
            node.AppendChild(ele);
            XmlElement nod = xdoc.CreateElement("Vehicles");
            node.AppendChild(nod);
            foreach (ClassVehicle car in data.Vehicles)
            {
                if (car.Number == null || car.Brand == null || car.Type == null || car.Number == "" || car.Brand == "" || car.Type == "")
                {
                    break;
                }
                XmlElement e = xdoc.CreateElement("Vehicle");
                e.SetAttribute("Number", car.Number);
                e.SetAttribute("Brand", car.Brand);
                e.SetAttribute("Type", car.Type);
                string project = "";
                if (car.Procj) { project += "a"; };
                if (car.Procs) { project += "b"; };
                if (car.Prohj) { project += "c"; };
                if (car.Pross) { project += "d"; };
                if (car.Procy) { project += "e"; };
                if (car.Progc) { project += "f"; };
                if (car.Prosx) { project += "g"; };
                if (car.Projsy) { project += "h"; };
                if (car.Profx) { project += "i"; };
                if (project == null || project == "") { project = "x"; };
                e.SetAttribute("Project", project);
                nod.AppendChild(e);
            }
            xdoc.Save(this.xmlPath);
        }

        private ClassData getDataFormNode(XmlNode node)
        {
            if (node == null || !node.HasChildNodes)
            {
                return null;
            }
            ClassData b = new ClassData();
            XmlElement xele = (XmlElement)node;
            b.ID = Int32.Parse(xele.GetAttribute("ID"));
            b.Code = xele.GetAttribute("Code");
            XmlNodeList nods = node.ChildNodes;
            XmlElement xel1 = (XmlElement)nods[0];
            b.CaseType = Int32.Parse(xel1.GetAttribute("CaseType"));
            b.Brigade = new Guid(xel1.GetAttribute("Brigade"));
            b.Contact = xel1.GetAttribute("Contact");
            b.User = new Guid(xel1.GetAttribute("User"));
            b.TimeHappen = DateTime.Parse(xel1.GetAttribute("TimeHappen"));
            string entrust = xel1.GetAttribute("TimeEntrust");
            if (entrust == null || entrust == "")
            {
                b.TimeEntrust = DateTime.MinValue;
            }
            else
            {
                b.TimeEntrust = DateTime.Parse(entrust);
            }
            b.TimeArrange = DateTime.Parse(xel1.GetAttribute("TimeArrange"));
            XmlNodeList nos = nods[1].ChildNodes;
            foreach (XmlNode no in nos)
            {
                ClassVehicle veh = new ClassVehicle();
                XmlElement car = (XmlElement)no;
                veh.Number = car.GetAttribute("Number");
                veh.Brand = car.GetAttribute("Brand");
                veh.Type = car.GetAttribute("Type");
                string project = car.GetAttribute("Project");
                if (project == null || project == "" || project == "x") { b.Vehicles.Add(veh); continue; };
                if (project.IndexOf('a') >= 0) { veh.Procj = true; };
                if (project.IndexOf('b') >= 0) { veh.Procs = true; };
                if (project.IndexOf('c') >= 0) { veh.Prohj = true; };
                if (project.IndexOf('d') >= 0) { veh.Pross = true; };
                if (project.IndexOf('e') >= 0) { veh.Procy = true; };
                if (project.IndexOf('f') >= 0) { veh.Progc = true; };
                if (project.IndexOf('g') >= 0) { veh.Prosx = true; };
                if (project.IndexOf('h') >= 0) { veh.Projsy = true; };
                if (project.IndexOf('i') >= 0) { veh.Profx = true; };
                b.Vehicles.Add(veh);
            }
            return b;
        }
    }
}
